# coding: utf-8
import json
import  HFUTclockin
import main


def signup(email,xuehao,passwd):
    global data
    temp_dict = {xuehao: { "email": email, "passwd": passwd}}
    with open("account.json", "r", encoding="utf-8") as f:
        old_data = json.load(f)
        if xuehao in old_data:
            if passwd == old_data[xuehao]['passwd']:
                if email == old_data[xuehao]['email']:
                    print("您已经加入自动打卡计划，无需重复加入")
                    data = {"info": "您已经加入自动打卡计划，无需重复加入"}
                else:
                    old_data[xuehao]['email'] =email
                    with open("account.json", "w", encoding="utf-8") as f:
                        js_data = json.dumps(old_data, indent=4, separators=(',', ':')).encode('utf-8').decode(
                            'raw_unicode_escape')
                        f.write(js_data)
                    print("您的邮箱已经更新")
                    data = {"info": "您的邮箱已经更新"}
            else:
                print("您已经加入自动打卡计划，如需修改邮箱密码，请删除账号后重新加入")
                data = {"info": "您已经加入自动打卡计划，如需修改邮箱密码，请删除账号后重新加入"}
        else:
            # 应该验证账号密码
            if verify(xuehao,passwd):
                old_data.update(temp_dict)
                with open("account.json", "w", encoding="utf-8") as f:
                    js_data = json.dumps(old_data, indent=4, separators=(',', ':')).encode('utf-8').decode(
                        'raw_unicode_escape')
                    f.write(js_data)
                    print("加入成功")
                    data = {"info": "账户验证成功，您已经加入自动打卡计划，每天下午两点自动打卡"}
            else:
                print("加入失败")
                data = {"info": "账户验证失败，学号或密码错误"}
        main.callback(data)


def del_account(xuehao):
    global data
    with open("account.json", "r", encoding="utf-8") as f:
        old_data = json.load(f)
        if xuehao in old_data:
            try:
                del old_data[xuehao]
                with open("account.json", "w", encoding="utf-8") as f:
                    js_data = json.dumps(old_data, indent=4, separators=(',', ':')).encode('utf-8').decode('raw_unicode_escape')
                    f.write(js_data)
                data = {"info": "删除账号成功，您已经退出自动打卡计划"}

            except:
                print("删除失败")
                data = {"info": "删除账号失败，请联系yx@iou.ink检查问题"}
        else:
            data = {"info": "此学号尚未加入自动打卡计划"}
        main.callback(data)


def verify(usn,psw):
    username = usn
    password = psw
    key = HFUTclockin.jump_auth_with_key()
    password = HFUTclockin.check_user_identy(username, password, key)
    print("login,name:",username)
    print("login,pwd:", password)
    ok = HFUTclockin.login(username, password)
    if ok:
        print("success")
        HFUTclockin.logout()
        return True
    else:
        print('登录失败哦....')
        HFUTclockin.logout()
        return False

if __name__ == '__main__':
    signup("1074637681@qq.com","2018211832","xxxxx")
#signup("10afasd81@yu.com","xssssswan")
#signin("1074637681@qq.com","xyxywasdas")
#signin("1074637asaffsdfsdgqq.com","xyxywan")
#signin("1074637681@qq.com","xyxywan")
#del_account("1074637681@qq.com","xyxywan")
